package com.sh.shanhuo.database;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import com.sh.shanhuo.database.sort.SortInfoEntity;
import com.sh.shanhuo.database.web.WebInfoEntity;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class SQLDBUtils {
    public static List<WebInfoEntity> getWebInfoDB(String dbPath) {
        List<WebInfoEntity> webInfoEntities = new ArrayList<>();
        File file = new File(dbPath);
        if (!file.exists()) {
            return webInfoEntities;
        }
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath, null);
        db.beginTransaction();
        String sql = "SELECT * FROM web_info";
        try {
            Cursor cursor = db.rawQuery(sql, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    WebInfoEntity webInfoEntity = new WebInfoEntity();
                    webInfoEntity._id = cursor.getLong(cursor.getColumnIndex("_id"));
                    webInfoEntity.updateTime = cursor.getLong(cursor.getColumnIndex("updateTime"));
                    webInfoEntity.webId = cursor.getString(cursor.getColumnIndex("webId"));
                    webInfoEntity.sortName = cursor.getString(cursor.getColumnIndex("sortName"));
                    webInfoEntity.webName = cursor.getString(cursor.getColumnIndex("webName"));
                    webInfoEntity.webLink = cursor.getString(cursor.getColumnIndex("webLink"));
                    webInfoEntity.description = cursor.getString(cursor.getColumnIndex("description"));
                    webInfoEntity.icon = cursor.getString(cursor.getColumnIndex("icon"));
                    webInfoEntity.onTop = cursor.getInt(cursor.getColumnIndex("onTop"));
                    webInfoEntity.serverTop = cursor.getInt(cursor.getColumnIndex("serverTop"));
                    webInfoEntity.isInServer = cursor.getInt(cursor.getColumnIndex("isInServer"));
                    webInfoEntity.permanent = cursor.getInt(cursor.getColumnIndex("permanent"));
                    webInfoEntity.fromBox = cursor.getInt(cursor.getColumnIndex("fromBox"));
                    webInfoEntity.lineMode = cursor.getInt(cursor.getColumnIndex("lineMode"));
                    webInfoEntity.deleteFlag = cursor.getInt(cursor.getColumnIndex("deleteFlag"));
                    webInfoEntities.add(webInfoEntity);
                }
                cursor.close();
            }
            //设置事务标志为成功，当结束事务时就会提交事务
            db.setTransactionSuccessful();
            //结束事务
            db.endTransaction();
            db.close();
        } catch (SQLiteException exception) {
            exception.printStackTrace();
        }
        return webInfoEntities;
    }

    public static List<SortInfoEntity> getSortDB(String dbPath) {
        List<SortInfoEntity> sortInfoEntities = new ArrayList<>();
        File file = new File(dbPath);
        if (!file.exists()) {
            return sortInfoEntities;
        }
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath, null);
        db.beginTransaction();
        String sql = "SELECT * FROM sort_info";
        try {
            Cursor cursor = db.rawQuery(sql, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    SortInfoEntity sortInfoEntity = new SortInfoEntity();
                    sortInfoEntity._id = cursor.getLong(cursor.getColumnIndex("_id"));
                    sortInfoEntity.sortName = cursor.getString(cursor.getColumnIndex("sortName"));
                    sortInfoEntity.sequence = cursor.getInt(cursor.getColumnIndex("sequence"));
                    sortInfoEntities.add(sortInfoEntity);
                }
                cursor.close();
            }
            //设置事务标志为成功，当结束事务时就会提交事务
            db.setTransactionSuccessful();
            //结束事务
            db.endTransaction();
            db.close();
        } catch (SQLiteException exception) {
            exception.printStackTrace();
        }
        return sortInfoEntities;
    }
}
